package com.imti.mall.sale.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.feign.clients.PmsProductCategoryFeignClient;
import com.imti.mall.model.pms.pojo.PmsProduct;
import com.imti.mall.model.sms.pojo.SmsFlashPromotionProductRelation;
import com.imti.mall.model.sms.vo.SmsCouponProductRelationTo;
import com.imti.mall.model.sms.vo.SmsFlashPromotionProductRelationVo;
import com.imti.mall.sale.service.SmsFlashPromotionProductRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/sale")
@Api(tags = "限时购与商品关系表管理")

public class SmsFlashPromotionProductRelationController  {

    @Autowired
    SmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    @Resource
    PmsProductCategoryFeignClient pmsProductCategoryFeignClient;

    /**
     * 查询限时购与商品关系表管理分类列表
     * @param
     * @return
     */
    @ApiOperation(value = "查询限时购限时购与商品关系表")
    @PostMapping(value = "/findSmsFlashPromotionProductRelationAndSmsFlashPromotionSession" )
    public CommonResult findSmsFlashPromotion
    ( SmsFlashPromotionProductRelationVo smsFlashPromotionProductRelationVo) {
        List<SmsFlashPromotionProductRelationVo> list = smsFlashPromotionProductRelationService.findSmsFlashPromotionProductRelationAndSmsFlashPromotionSession(smsFlashPromotionProductRelationVo);
        if(list.size()>0){
            return CommonResult.success(CommonPage.restPage(list));
        }else {
            return CommonResult.success("未查询到信息");
        }
    }

    @ApiOperation("添加限时购与商品关系表")
    @PostMapping(value = "/addSmsFlashPromotionProductRelation")
    public CommonResult addSmsFlashPromotionProductRelation(@RequestBody List<SmsFlashPromotionProductRelation> list) {
        Integer req = smsFlashPromotionProductRelationService.addSmsFlashPromotionProductRelation(list);
        if(req>0){
            return CommonResult.success("添加成功");
        }else {
            return CommonResult.failed("添加失败");
        }
    }

    @ApiOperation("修改限时购与商品关系表")
    @PostMapping(value = "/updateSmsFlashPromotionProductRelation/{id}")
    public CommonResult updateSmsFlashPromotionProductRelation(@PathVariable("id") Long id, @RequestBody SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        Integer req = smsFlashPromotionProductRelationService.updateSmsFlashPromotionProductRelation(smsFlashPromotionProductRelation);
        if(req>0){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }
    }

    @ApiOperation("添加限时购与商品关系表")
    @PostMapping(value = "/deleteSmsFlashPromotionProductRelation")
    public CommonResult deleteSmsFlashPromotionProductRelation(@PathVariable("id") Long id) {
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation=new SmsFlashPromotionProductRelation();
        smsFlashPromotionProductRelation.setId(id);
        Integer req = smsFlashPromotionProductRelationService.deleteSmsFlashPromotionProductRelation(smsFlashPromotionProductRelation);
        if(req>0){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }
    }

    /**
     * 查询限时购与商品关系表列表
     * @param smsCouponProductRelationTo
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "查询限时购与商品关系表列表")
    @PostMapping(value = "/findSmsFlashPromotionProductRelation" )
    public CommonResult findSmsFlashPromotionProductRelation
    (  SmsCouponProductRelationTo smsCouponProductRelationTo,
      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<SmsCouponProductRelationTo> list = smsFlashPromotionProductRelationService.findSmsFlashPromotionProductRelation(smsCouponProductRelationTo,pageSize, pageNum);
        for (int i=0;i<list.size();i++){
            PmsProduct product =pmsProductCategoryFeignClient.getProductToSale(list.get(i).getProductId().intValue());
            list.get(i).setProduct(product);
        }
        if(list.size()>0){
            return CommonResult.success(CommonPage.restPage(list));
        }else {
            return CommonResult.success("未查询到信息");
        }
    }
}
